package com.superjson.showpro.dao.impl;

import com.superjson.showpro.base.GenericDaoImpl;
import com.superjson.showpro.entity.Comment;
import com.superjson.showpro.dao.CommentDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.beantools.util.ObjectUtil.isNotNull;

@Repository
public class CommentDaoImpl extends GenericDaoImpl<Comment> implements CommentDao {

    public CommentDaoImpl() {
        super(Comment.class);
    }

    @Override
    public List<Comment> listComment(Long articleId) {
        String sql = "SELECT " +
                " * " +
                "FROM " +
                " sp_comment " +
                "WHERE " +
                " is_deleted = 0" +
                (isNotNull(articleId) ? " AND article_catalog_id = " + articleId : "") +
                " ORDER BY create_datetime DESC ";
        Query query = getSession().createSQLQuery(sql).addEntity(Comment.class);
        return query.list();
    }

    @Override
    public List<Comment> listComment(Long userId, Long articleId) {
        String sql = "SELECT " +
                " * " +
                "FROM " +
                " sp_comment " +
                "WHERE " +
                " is_deleted = 0" +
                (isNotNull(userId) ? " AND user_id = " + userId : "") +
                (isNotNull(articleId) ? " AND article_catalog_id = " + articleId : "") +
                " ORDER BY create_datetime DESC ";
        Query query = getSession().createSQLQuery(sql).addEntity(Comment.class);
        return query.list();
    }
}